package pl.edu.agh.iosr.gamblingzone.action;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

import pl.edu.agh.iosr.gamblingzone.model.Participant;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.dao.ParticipantDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.ParticipantDAOBean;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.UserDAOBean;

@Stateless
@Name("participantsManage")
public class ParticipantsManageAction implements ParticipantsManage {

	@PersistenceContext
	EntityManager entityManager;

	private List<Participant> allParticipants;

	private ParticipantDAO participantDAO;

	private void setupDAO() {
		if (participantDAO == null) {
			participantDAO = new ParticipantDAOBean();
			((ParticipantDAOBean) participantDAO)
					.setEntityManager(entityManager);
		}
	}

	@Out
	public List<Participant> getAllParticipants() {
		setupDAO();
		allParticipants = participantDAO.findAll();

		// workaround
		if (allParticipants == null) {
			allParticipants = new ArrayList<Participant>();
		}
		return allParticipants;
	}

	public void removeParticipant(Participant p) {
		participantDAO.makeTransient(p);
	}

	public void addParticipant() {
		setupDAO();
		Participant p = new Participant();
		p.setName("t1");
		p.setDescription("t2");
		participantDAO.makePersistent(p);

	}

}
